package com.survey.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.survey.common.core.domain.entity.SysUser;
import com.survey.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.survey.common.annotation.Log;
import com.survey.common.core.controller.BaseController;
import com.survey.common.core.domain.AjaxResult;
import com.survey.common.enums.BusinessType;
import com.survey.system.domain.SurIssue;
import com.survey.system.service.ISurIssueService;
import com.survey.common.utils.poi.ExcelUtil;
import com.survey.common.core.page.TableDataInfo;

/**
 * 问题信息Controller
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/system/issue")
public class SurIssueController extends BaseController {
    @Autowired
    private ISurIssueService surIssueService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询问题信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:issue:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurIssue surIssue) {
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (sysUser.getSchoolId() != 0) {
            surIssue.setSchoolId(sysUser.getSchoolId());
        }
        startPage();
        return getDataTable(surIssueService.selectSurIssueList(surIssue));
    }

    /**
     * 导出问题信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:issue:export')")
    @Log(title = "问题信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurIssue surIssue) {
        List<SurIssue> list = surIssueService.selectSurIssueList(surIssue);
        ExcelUtil<SurIssue> util = new ExcelUtil<SurIssue>(SurIssue.class);
        util.exportExcel(response, list, "问题信息数据");
    }

    /**
     * 获取问题信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:issue:query')")
    @GetMapping(value = "/{issueId}")
    public AjaxResult getInfo(@PathVariable("issueId") Long issueId) {
        return success(surIssueService.selectSurIssueByIssueId(issueId));
    }

    /**
     * 新增问题信息
     */
    @PreAuthorize("@ss.hasPermi('system:issue:add')")
    @Log(title = "问题信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurIssue surIssue) {
        surIssue.setCreateBy(getUsername());
        return toAjax(surIssueService.insertSurIssue(surIssue));
    }

    /**
     * 修改问题信息
     */
    @PreAuthorize("@ss.hasPermi('system:issue:edit')")
    @Log(title = "问题信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurIssue surIssue) {
        surIssue.setUpdateBy(getUsername());
        return toAjax(surIssueService.updateSurIssue(surIssue));
    }

    /**
     * 删除问题信息
     */
    @PreAuthorize("@ss.hasPermi('system:issue:remove')")
    @Log(title = "问题信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{issueIds}")
    public AjaxResult remove(@PathVariable Long[] issueIds) {
        return toAjax(surIssueService.deleteSurIssueByIssueIds(issueIds));
    }

    /**
     * 根据问题id获取反馈
     */
    @PreAuthorize("@ss.hasPermi('system:issue:query')")
    @GetMapping(value = "/getFeedBackByIssueId/{issueId}")
    public AjaxResult getFeedback(@PathVariable("issueId") Long issueId) {
        System.out.println("issueId" + issueId);
        return success(surIssueService.selectSurFeedbackByIssueId(issueId));
    }
}
